package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.SeniorExpertAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.Expert;
import com.isunland.managesystem.entity.SeniorExpert;
import com.isunland.managesystem.entity.SeniorExpertOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/13 18:00
 */
public class SeniorExpertListFragment extends BaseListFragment
    implements SeniorExpertAdapter.Callback {

  private ArrayList<SeniorExpert> mList;
  private SeniorExpert mExpert;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);

    mList = new ArrayList<>();
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);
  }

  @Override public String getUrl() {
    return ApiConst.URL_EXPERT;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("beginregDate", "");
    params.put("endregDate", "");
    params.put("exportName", "");
    params.put("exportKindCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));//区分农技和农机专家
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("dataStatus", DataStatus.PUBLIS);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    SeniorExpertOriginal original = new Gson().fromJson(json, SeniorExpertOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      SeniorExpertAdapter mAdapter = new SeniorExpertAdapter(getActivity(), mList, this);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //可以查看专家发布的帖子和回复
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    mExpert = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), AgriculturalMyExpertPagerActivity.class);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM, AgriculturalForumListFragment.PERSON);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_JOBNO, mExpert.getExportJobno());
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void clickView(int type, int position) {
    mExpert = mList.get(position);
    switch (type) {
      case 0:
        if (mExpert.getRemark() != null && "T".equalsIgnoreCase(mExpert.getRemark())) {
          //已关注: 取消关注
          volleyPostCancleAttention();
        } else {
          //关注
          volleyPostAttention();
        }
        break;

      case 1:
        //咨询
        startAskActivity();
        break;
      default:
        break;
    }
  }

  private void startAskActivity() {
    Expert mExpertContent = new Expert();
    mExpertContent.setMembername(mExpert.getExportName());
    mExpertContent.setJobno(mExpert.getExportJobno());
    Intent intent = new Intent(getActivity(), AgriculturalTopicPublishActicity.class);
    //intent.putExtra(AgriculturalTopicPublishFragment.EXTRA_FORUM_TYPE, mCompanyType);
    intent.putExtra(AgriculturalTopicPublishFragment.EXTRA_TYPE,
        AgriculturalTopicPublishFragment.SELF);
    intent.putExtra(AgriculturalTopicPublishFragment.EXTRA_FROM, mExpertContent);
    startActivity(intent);
  }

  /* 取消关注 */
  private void volleyPostCancleAttention() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_EXPERT_CANCLE_ATTENTION);
    HashMap<String, String> params = new HashMap<>();
    params.put("toAddJobno", mExpert.getExportJobno());
    MyUtils.showDialogLoading(getActivity(), "正在取消关注");
    mActivity.volleyPost(url, params, getResponse());
  }

  /*关注*/
  private void volleyPostAttention() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_EXPERT_ATTENTION);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("toAddJobno", mExpert.getExportJobno());
    params.put("friendOrFollow", "F");
    params.put("subCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));//区分农技或农机
    MyUtils.showDialogLoading(getActivity(), "正在关注");
    mActivity.volleyPost(url, params.getParams(), getResponse());
  }

  private VolleyResponse getResponse() {
    return new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String json) throws JSONException {

        MyUtils.dismissDialog();
        try {
          SuccessMessage successsMessage = new Gson().fromJson(json, SuccessMessage.class);

          String successType = successsMessage.getResult();

          if (successType.equals("1")) {
            ToastUtil.show(R.string.success_operation);
            volleyPost();
          } else {
            ToastUtil.show(successsMessage.getMessage());
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.failure_operation);
        }
      }
    };
  }
}
